<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentCode">
        <!-- <el-input v-model="form.studentCode" placeholder="请输入学号" /> -->
        <el-autocomplete class="inline-input" value-key="studentCode"
          v-model="queryParams.studentCode"
          :fetch-suggestions="queryStudentCode"
          placeholder="请输入学号"
          @select="handleQueryStudentCode"></el-autocomplete>
      </el-form-item>
      <el-form-item label="学员姓名" prop="studentName">
        <!-- <el-input v-model="form.studentName" placeholder="请输入学员姓名" /> -->
        <el-autocomplete class="inline-input" value-key="studentName"
          v-model="queryParams.studentName"
          :fetch-suggestions="queryStudentName"
          placeholder="请输入学员姓名"
          @select="handleQueryStudentName"></el-autocomplete>
      </el-form-item>
      <el-form-item label="教练" prop="coachName">
        <el-autocomplete class="inline-input" value-key="coachName"
          v-model="queryParams.coachName"
          :fetch-suggestions="queryCoachName"
          placeholder="请输入教练姓名"
          @select="handleQueryCoachName"></el-autocomplete>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['puyu:strengthtest:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['puyu:strengthtest:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['puyu:strengthtest:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['puyu:strengthtest:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="strengthtestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测试ID" align="center" prop="strengthTestId" />
      <el-table-column label="测试时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}.{m}.{d}') }}</span>
        </template>
      </el-table-column>
     <!-- <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column label="学员ID" align="center" prop="studentId" /> -->
      <el-table-column label="学号" align="center" prop="studentCode">
        <template slot-scope="scope">
          <span>{{ getStudentCode(scope.row.studentId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学员姓名" align="center" prop="studentName">
        <template slot-scope="scope">
          <span>{{ getStudentName(scope.row.studentId) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="教练ID" align="center" prop="coachId" /> -->
      <el-table-column label="教练" align="center" prop="coachName">
        <template slot-scope="scope">
          <span>{{ getCoachName(scope.row.coachId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身高(cm)" align="center" prop="height" />
      <el-table-column label="体重(kg)" align="center" prop="weight" />
      <el-table-column label="摸高(cm)" align="center" prop="touchHigh" />
      <el-table-column label="立定(cm)" align="center" prop="standingLeap" />
      <el-table-column label="一分钟跳绳-单摇(n)" align="center" prop="ropeSkipping" />
      <el-table-column label="一分钟跳绳--双摇(n)" align="center" prop="ropeSkippingDouble" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['puyu:strengthtest:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['puyu:strengthtest:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改体能测试对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="学号" prop="studentCode">
          <el-autocomplete class="inline-input" value-key="studentCode"
            v-model="form.studentCode"
            :fetch-suggestions="queryStudentCode"
            placeholder="请输入学号"
            @select="handleStudentCode"
            :disabled="form.strengthTestId != undefined"></el-autocomplete>
        </el-form-item>
        <el-form-item label="学员姓名" prop="studentName">
          <el-autocomplete class="inline-input" value-key="studentName"
            v-model="form.studentName"
            :fetch-suggestions="queryStudentName"
            placeholder="请输入学员姓名"
            @select="handleStudentName"
            :disabled="form.strengthTestId != undefined"></el-autocomplete>
        </el-form-item>
        <el-form-item label="身高(cm)" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高cm" />
        </el-form-item>
        <el-form-item label="体重(kg)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重kg" />
        </el-form-item>
        <el-form-item label="摸高(cm)" prop="touchHigh">
          <el-input v-model="form.touchHigh" placeholder="请输入摸高cm" />
        </el-form-item>
        <el-form-item label="立定(cm)" prop="standingLeap">
          <el-input v-model="form.standingLeap" placeholder="请输入立定cm" />
        </el-form-item>
        <el-form-item label="一分钟跳绳-单摇(n)" prop="ropeSkipping">
          <el-input v-model="form.ropeSkipping" placeholder="请输入一分钟跳绳-单摇n" />
        </el-form-item>
        <el-form-item label="一分钟跳绳-双摇(n)" prop="ropeSkippingDouble">
          <el-input v-model="form.ropeSkippingDouble" placeholder="请输入一分钟跳绳-双摇n" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStrengthtest, getStrengthtest, delStrengthtest, addStrengthtest, updateStrengthtest } from "@/api/puyu/strengthtest";
import { listStudents } from "@/api/puyu/student";
import { listCoachs } from "@/api/puyu/coach";

export default {
  name: "Strengthtest",
  dicts: ['sys_del_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 体能测试表格数据
      strengthtestList: [],
      // 学员表格数据
      studentList: null,
      // 教练数据
      coachList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        studentId: null,
        coachId: null,
        height: null,
        weight: null,
        touchHigh: null,
        standingLeap: null,
        ropeSkipping: null,
        ropeSkippingDouble: null,
        doubleRunning: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    // 获取教练下拉列表
    this.getCoachList();
    // 获取学员下拉列表
    this.getStudentLsit();
  },
  methods: {
    /** 查询体能测试列表 */
    getList() {
      this.loading = true;
      listStrengthtest(this.queryParams).then(response => {
        this.strengthtestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询教练用户列表 */
    getCoachList() {
      listCoachs().then(response => {
        this.coachList = response.data;
      });
    },
    /** 查询学员列表 */
    getStudentLsit() {
      listStudents().then(response => {
        this.studentList = response.data;
      });
    },
    // 根据coachId获取教练姓名
    getCoachName(coachId) {
      if (this.coachList) {
        return this.coachList.filter(item => item.coachId == coachId)[0].coachName;
      }
      return '';
    },
    // 根据studentId获取学号
    getStudentCode(studentId) {
      if (this.studentList) {
        return this.studentList.filter(item => item.studentId == studentId)[0].studentCode;
      }
      return '';
    },
    // 根据studentId获取学员姓名
    getStudentName(studentId) {
      if (this.studentList) {
        return this.studentList.filter(item => item.studentId == studentId)[0].studentName;
      }
      return '';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        strengthTestId: null,
        deptId: null,
        studentId: null,
        coachId: null,
        height: null,
        weight: null,
        touchHigh: null,
        standingLeap: null,
        ropeSkipping: null,
        doubleRunning: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.strengthTestId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加体能测试";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const strengthTestId = row.strengthTestId || this.ids
      getStrengthtest(strengthTestId).then(response => {
        this.form = response.data;
        this.form.studentCode = this.getStudentCode(this.form.studentId);
        this.form.studentName = this.getStudentName(this.form.studentId);
        this.open = true;
        this.title = "修改体能测试";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.strengthTestId != null) {
            updateStrengthtest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStrengthtest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const strengthTestIds = row.strengthTestId || this.ids;
      this.$modal.confirm('是否确认删除体能测试编号为"' + strengthTestIds + '"的数据项？').then(function() {
        return delStrengthtest(strengthTestIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('puyu/strengthtest/export', {
        ...this.queryParams
      }, `strengthtest_${new Date().getTime()}.xlsx`)
    },
    queryStudentCode(queryString, cb) {
        var studentList = this.studentList;
        var results = queryString
            ? studentList.filter(this.filterStudentCode(queryString))
            : studentList;
        // 调用 callback 返回建议列表的数据
        cb(results);
    },
    filterStudentCode(queryString) {
        return (student) => {
            return (
                student.studentCode
                    .toLowerCase()
                    .indexOf(queryString.toLowerCase()) > -1
            );
        };
    },
    //选中学号
    handleStudentCode(item) {
        this.form.userId = item.userId;
        this.form.deptId = item.deptId;
        this.form.studentId = item.studentId;
        this.form.studentCode = item.studentCode;
        this.form.studentName = item.studentName;
    },
    handleQueryStudentCode(item) {
        this.queryParams.studentId = item.studentId;
        this.queryParams.studentName = item.studentName;
    },
    queryStudentName(queryString, cb) {
        var studentList = this.studentList;
        var results = queryString
            ? studentList.filter(this.filterStudentName(queryString))
            : studentList;
        // 调用 callback 返回建议列表的数据
        cb(results);
    },
    filterStudentName(queryString) {
        return (student) => {
            return (
                student.studentName
                    .toLowerCase()
                    .indexOf(queryString.toLowerCase()) > -1
            );
        };
    },
    //选中姓名
    handleStudentName(item) {
        this.form.userId = item.userId;
        this.form.deptId = item.deptId;
        this.form.studentId = item.studentId;
        this.form.studentCode = item.studentCode;
        this.form.studentName = item.studentName;
    },
    handleQueryStudentName(item) {
        this.queryParams.studentCode = item.studentCode;
        this.queryParams.studentId = item.studentId;
    },
    queryCoachName(queryString, cb) {
        var coachList = this.coachList;
        var results = queryString
            ? coachList.filter(this.filterCoachName(queryString))
            : coachList;
        // 调用 callback 返回建议列表的数据
        cb(results);
    },
    filterCoachName(queryString) {
        return (coach) => {
            return (
                coach.coachName
                    .toLowerCase()
                    .indexOf(queryString.toLowerCase()) > -1
            );
        };
    },
    //选中教练
    handleQueryCoachName(item) {
        this.queryParams.coachId = item.coachId;
    },
  }
};
</script>
